<template>
  <!--  无卡充值类型 -->
  <div class="swipe_details">
    <div class="fanshionable_title">
      <span>充值类型</span>
    </div>
    <el-form :inline="true" :model="elform" class="demo-form-inline1">
      <el-form-item>
        <el-button
          type="primary"
          @click="showPass(true)"
          size="mini"
          icon="el-icon-plus"
          >添加</el-button
        >
      </el-form-item>
      <!-- <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          >查询</el-button
        >
      </el-form-item> -->
    </el-form>
    <div class="tablemain">
      <el-table
        v-loading="loading"
        :data="detailslist"
        ref="table"
        border
        highlight-current-row
        style="width: 100%"
        :header-cell-style="headFirst"
        :cell-style="(res) => headFirst_col(res, [])"
      >
        <el-table-column
          prop="typeCode"
          label="充值序号"
          width="80px"
        ></el-table-column>
        <el-table-column prop="typeName" label="类型名称"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="showPass(false, scope.row)"
              type="text"
              size="small"
              >修改</el-button
            >
            <!-- <el-button
              @click="handleEle(scope.row)"
              type="text"
              size="small"
              style="color: red"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChangepage"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="recordCount"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :title="operation ? '添加充值类型' : '修改充值类型'"
      :visible.sync="subscribe"
      width="500px"
      center
      :close-on-click-modal="false"
      :before-close="closeDialog"
      append-to-body
    >
      <el-form
        :model="addFrom"
        :rules="rules"
        ref="addFrom"
        label-width="100px"
      >
        <el-form-item label="充值序号:" prop="typeCode" v-if="!operation">
          <el-col :span="20">
            <el-input
              size="mini"
              v-model.number="addFrom.typeCode"
              style="width: 180px"
              oninput="value=value.replace(/[^\d]/g,'')"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="类型名称:" prop="typeName">
          <el-col :span="20">
            <el-input
              size="mini"
              v-model="addFrom.typeName"
              style="width: 180px"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-col :span="20">
            <el-input
              type="textarea"
              size="mini"
              v-model="addFrom.remark"
              style="width: 180px"
            ></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="subscribeAdd()" v-if="operation"
          >确 定</el-button
        >
        <el-button type="primary" @click="subscribeEdit()" v-if="!operation"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
  data() {
    return {
      elform: {},
      //  无卡充值类型数据
      detailslist: [],
      loading: false,
      pageNum: 1,
      pageSize: 20,
      currentPage: 1,
      recordCount: 0,
      searchInfo: {},
      formThead: {
        cardNumber: { label: "卡号", prop: "cardNumber", checked: true },
        userNumber: { label: "编号", prop: "userNumber", checked: true },
        userName: { label: "姓名", prop: "userName", checked: true },
        dataMakeTime: {
          label: "通行时间",
          prop: "dataMakeTime",
          checked: true,
        },
        roomName: { label: "房间", prop: "place", checked: true },
        place: { label: "所在位置", prop: "place", checked: true },
        dataType: { label: "开门类型", prop: "dataType", checked: true },

        departmentName: {
          label: "部门",
          prop: "departmentName",
          checked: true,
        },
        chargingType: {
          label: "交易类型",
          prop: "chargingType",
          checked: true,
        },
        expenditure: { label: "操作金额", prop: "expenditure", checked: true },
        originBalance: {
          label: "卡内余额",
          prop: "originBalance",
          checked: true,
        },
      },
      //导出数据
      detailslistExportlist: [],
      /* 弹框显示*/
      // true为添加，false为修改
      operation: true,
      subscribe: false,
      addFrom: {
        /**
         * 充值序号
         */
        typeCode: "",
        /**
         * 类型名称
         */
        typeName: "",
        /**
         * 备注
         */
        remark: "",
      },
      rules: {
        // typeCode: [
        //   { required: true, message: "请输入充值序号", trigger: "blur" },
        // ],
        typeName: [
          { required: true, message: "请输入类型名称", trigger: "blur" },
        ],
      },
    };
  },
  props: ["roomGuid"],
  created() {
    this.userGuid = sessionStorage.getItem("administrator");
    this.onSubmit();
  },
  mounted() {},
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      },
    },
  },
  methods: {
    /**
     * 调整类型 1-减款，2-充值
     *private Integer adjustType;
     *
     */
    // 取消添加弹窗
    closeDialog() {
      this.$refs["addFrom"].resetFields();
      this.subscribe = false;
    },
    // 点击添加后弹出的窗口
    showPass(type, row) {
      this.subscribe = true;
      if (type) {
        this.operation = true;
        this.$nextTick(() => {
          this.$refs["addFrom"].resetFields();
        });
      } else {
        this.operation = false;
        // this.$set(this.addFrom, "typeCode", row.typeCode);
        // this.$set(this.addFrom, "typeName", row.typeName);
        // this.$set(this.addFrom, "remark", row.remark);
        this.addFrom = { ...row };
      }
    },
    // 确定添加
    subscribeAdd() {
      this.$http({
        url: this.$http.adornUrl("/unit/recharge-deduction-type-add"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            remark: this.addFrom.remark,
            typeName: this.addFrom.typeName,
            unitGuid: this.changeschoolid,
            adjustType: 2,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.getList();
          this.subscribe = false;
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    // 确定修改
    subscribeEdit() {
      this.$http({
        url: this.$http.adornUrl("/unit/recharge-deduction-type-update"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            pkTypeId: this.addFrom.pkTypeId,
            typeCode: this.addFrom.typeCode,
            typeName: this.addFrom.typeName,
            remark: this.addFrom.remark,
            unitGuid: this.changeschoolid,
            adjustType: 2,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.getList();
          this.subscribe = false;
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    // 删除
    handleEle(row) {},
    // 查询
    onSubmit() {
      let searchInfo = {};
      searchInfo.unitGuid = this.changeschoolid;
      searchInfo.adjustType = 2;
      this.searchInfo = searchInfo;
      this.pageNum = 1;
      this.getList();
    },

    // 获取数据
    getList() {
      this.$http({
        url: this.$http.adornUrl("/unit/recharge-deduction-type-page"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            asc: true,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          //   this.$message({
          //     message: "查询成功",
          //     type: "success",
          //   });

          this.recordCount = data.messageContent.recordCount;
          this.currentPage = data.messageContent.pageNum;
          this.detailslist = data.messageContent.data;
          console.log(data.messageContent.data);
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChangepage(val) {
      this.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.swipe_details {
  padding: 10px;
  overflow: hidden;
  .fanshionable_title {
    margin-top: 20px;
    height: 30px;
    color: #909399;
    border-bottom: 1px solid #e5e9f2;
  }
  //滚动条的宽度
  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  //滚动条的滑块
  ::-webkit-scrollbar-thumb {
    background-color: #a1a3a9;
    border-radius: 3px;
  }
  .el-table__row .el-table__row--level-1 {
    background-color: #e5e9f2;
  }
  /deep/.el-dialog--center .el-dialog__body {
    padding-left: 70px;
  }
  .demo-form-inline,
  .demo-form-inline1 {
    height: 40px;
    display: flex;
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 0px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 340px;
    }
  }
  .demo-form-inline1 {
    border-bottom: 1px solid #e5e9f2;
    .searchmarigin {
      margin-left: 20px;
    }
  }
  .demo-ruleForm {
    margin-left: -70px;
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>